using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Occupie.Models;
using Occupie.Managers;
using WebMatrix.WebData;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Occupie.ViewModels;
using System.Web.Helpers;
using System.IO;

namespace Occupie.Controllers
{
    public class EmployerController : BaseController
    {
        //
        // GET: /Employer/Profile

        public ActionResult Profile(int id = 0)
        {
            Employer employer;
            if (id == 0)
            {
                employer = employerManager.GetEmployerByUserId(WebSecurity.CurrentUserId);
            }
            else
            {
                employer = employerManager.GetEmployerByUserId(id);
            }

            if (!employer.IsActive)
            {
                return new HttpNotFoundResult();
            }

            return View(employer);
        }


        //
        // GET: /Employer/Edit

        public ActionResult Edit()
        {
            Employer employer = employerManager.GetEmployerByUserId(WebSecurity.CurrentUserId);
            if (!employer.IsActive)
            {
                return new HttpNotFoundResult();
            }
            return View(employer);
        }

        //
        // POST: /Employer/Edit
        [HttpPost]
        public ActionResult Edit(Employer employer)
        {
            if (ModelState.IsValid)
            {
                if (employerManager.SaveEmployer(employer) != null)
                {
                    return RedirectToAction("Profile");
                }
            }

            return View(employer);
        }

        //
        // GET: /Employer/All

        public ActionResult All()
        {
            return View(employerManager.GetEmployers());
        }

        public ActionResult ReadEmployers([DataSourceRequest] DataSourceRequest request)
        {
            return Json(employerManager.GetEmployers().ToDataSourceResult(request));
        }

        [HttpGet]
        public ActionResult UploadImage(int id)
        {
            ViewBag.UserId = id;
            return PartialView("_UploadPartial");
        }

        [HttpPost]
        public ActionResult FileUpload(int id)
        {
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                          "Content\\Employers\\Logos");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            for (int i = 0; i < Request.Files.Count; i++)
            {
                HttpPostedFileBase hpf = Request.Files[i] as HttpPostedFileBase;

                if (Path.GetFileName(hpf.FileName) == String.Empty)
                {
                    continue;
                }

                string fileName = Path.Combine(path, id + "_" + Path.GetFileName(hpf.FileName));
                hpf.SaveAs(fileName);

                db.Employers.First(x => x.UserId == id).LogoPath = "/Content/Employers/Logos/" + id + "_" + hpf.FileName;
                db.SaveChanges();
            }

            return RedirectToAction("Profile");
        }
    }
}
